import axios from 'axios'
import router from '@/router'

const request = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL
})

request.interceptors.request.use(config => {
  config.headers['Authorization'] = localStorage.getItem('token')
  return config
})

request.interceptors.response.use(response => {
  if (response.data.code === 401) {
    router.push('/login')
  }
  return response.data
})

export default request